package ltd.followkba.comment.utils;

import android.util.Log;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import main.MAPI;

public class CFileDbs {
    RandomAccessFile mAccessFile = null;
    public int mInput = 0;
    public int mOutput = 0;
    public int mCounter = 0;
    public int mLength = 0;
    public CFileDbs(){
    }
    public CFileDbs(String fileName){
        setFileName(fileName);
    }
    public void setFileName(String fileName){
        if (fileName != null) {
            if (MAPI.GET_FILE_LENGTH(fileName) < 16) {
                byte[] gpLocal_1 = new byte[16];
                MAPI.WRITE_BUFF8_FILE(false, 16, 0, gpLocal_1, fileName);
            }
            try {
                mAccessFile = new RandomAccessFile(fileName, "rw");
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            try {
                if (mAccessFile != null) {
//MSTRING("CFileDbsB "+ fileName);
                    mLength = (int) mAccessFile.length();
                    mOutput = mAccessFile.readInt();        // [0]OUT[1]IN[2]榜计数
                    mInput = mAccessFile.readInt();
                    mCounter = mAccessFile.readInt();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    public void seek(int offset){

        if (mAccessFile != null) {
            try {
                mAccessFile.seek(16 + (offset * 4));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void writeInt(int value){
        try {
            mAccessFile.writeInt(value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void writeInt(int offset, int value){
        if (mAccessFile != null) {
            try {
                int g4Local_1 = 16 + (offset * 4);
                mAccessFile.seek(g4Local_1);
                mAccessFile.writeInt(value);
                if (g4Local_1 >= mLength) {
                    mLength = g4Local_1 + 4;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void addInt(int value){
        if (mAccessFile != null) {
            try {
                mAccessFile.seek(mLength);
                mAccessFile.writeInt(value);
                mLength += 4;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void writeOutput(){
        if (mAccessFile != null) {
            try {
                mAccessFile.seek(0);
                mAccessFile.writeInt(mOutput);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void writeInput(){
        if (mAccessFile != null) {
            try {
                mAccessFile.seek(4);
                mAccessFile.writeInt(mInput);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void writeCounter(){
        if (mAccessFile != null) {
            try {
                mAccessFile.seek(8);
                mAccessFile.writeInt(mCounter);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public int readInt(){
        if (mAccessFile != null) {
            try {
                return mAccessFile.readInt();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
    public int readInt(int offset){
        if (mAccessFile != null) {
            try {
                mAccessFile.seek(16 + (offset * 4));
                return mAccessFile.readInt();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
    public void close(){
        try {
            if (mAccessFile != null) {
                mAccessFile.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void SetUserID4Byte(String userID, int parameter){
        SetUserID4Byte(MAPI.STRING_DEC32(userID), parameter);
    }
    public void SetUserID4Byte(int userID, int parameter){
        if (mAccessFile != null) {
            try {
                int g4Local_1 = 16;
                int g4Local_2;
                while (g4Local_1 < mLength) {
                    mAccessFile.seek(g4Local_1);
                    g4Local_2 = mAccessFile.readInt();
                    if (g4Local_2 == userID) {
                        break;
                    }
                    g4Local_1 += 8;
                }
                if (g4Local_1 == mLength) {                     // 还没有存在
                    mAccessFile.seek(g4Local_1);
                    mAccessFile.writeInt(userID);
                    mLength += 8;
                } else {
                    mAccessFile.seek(g4Local_1 + 4);
                }
                mAccessFile.writeInt(parameter);
            } catch (IOException e) {
            }
        }
    }
    public int GetUserID4Byte(String userID){
        return GetUserID4Byte(MAPI.STRING_DEC32(userID));
    }
    public int GetUserID4Byte(int userID){
        if (mAccessFile != null) {
            try {
                int g4Local_1 = 16;
                int g4Local_2;
                while (g4Local_1 < mLength) {
                    mAccessFile.seek(g4Local_1);
                    g4Local_2 = mAccessFile.readInt();
                    if (g4Local_2 == userID) {
                        return mAccessFile.readInt();
                    }
                    g4Local_1 += 8;
                }
            } catch (IOException e) {
            }
        }
        return -1;
    }
    public void MSTRING(String string) {
        Log.d("MSYS", string);
    }
    public void MDWORD(int g4Local_1) {
        Log.d("MSYS", String.format("%08X", g4Local_1));
    }}
